﻿using System;
using System.Collections.ObjectModel;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;

namespace MCP.Data.CF_Entities
{
    public class MCPDB : DbContext
    {
        public MCPDB()
            : base("MCP.Data.CF_Entities.MCPEntities")
        { }

        public DbSet<User> Users { get; set; }
        public DbSet<Check> Checks { get; set; }
        public DbSet<Item> Items { get; set; }
        //public DbSet<ItemCategory> ItemCategories { get; set; }
        public DbSet<Category> Categories { get; set; }
    }

    public class MCPDBConfiguration : DbMigrationsConfiguration<MCPDB>
    {
        public MCPDBConfiguration()
        {
            this.AutomaticMigrationsEnabled = true;
            this.AutomaticMigrationDataLossAllowed = true;
        }

        protected override void Seed(MCPDB db)
        {
            try
            {
                if (!db.Users.Any())
                {
                    db.Users.Add(new User { Username = "user", Password = "user", Settings = new Settings() });
                    db.Users.Add(new User { Username = "admin", Password = "admin", Settings = new Settings() });

                    db.Categories.Add(new Category { CategoryName = "food" });
                    db.Categories.Add(new Category { CategoryName = "required" });
                    db.Categories.Add(new Category { CategoryName = "meat" });
                    db.Categories.Add(new Category { CategoryName = "cell phone" });
                    db.Categories.Add(new Category { CategoryName = "tech" });
                    db.Categories.Add(new Category { CategoryName = "internet" });

                    db.SaveChanges();

                    db.Checks.Add(new Check
                        {
                            UserID = db.Users.SingleOrDefault(u => u.Username == "user").ID,
                            TotalPrice = 114.15,
                            Date = DateTime.Now.AddMonths(-3).AddDays(-6),
                            Items = new Collection<Item>
                                {
                                    new Item
                                        {
                                            Name = "Bread",
                                            Amount = 1,
                                            UnitPrice = 5.4,
                                            Price = 5.4,
                                            Categories = new Collection<Category>
                                                {
                                                    db.Categories.SingleOrDefault(c => c.CategoryName == "food"),
                                                    db.Categories.SingleOrDefault(c => c.CategoryName == "required")
                                                },

                                        },
                                    new Item
                                        {
                                            Name = "Chicken meat",
                                            Amount = 2.5,
                                            UnitPrice = 43.5,
                                            Price = 43.5*2.5,
                                            Categories = new Collection<Category>
                                                {
                                                    db.Categories.SingleOrDefault(c => c.CategoryName == "food"),
                                                    db.Categories.SingleOrDefault(c => c.CategoryName == "meat")
                                                }
                                        }
                                }
                        });
                    db.SaveChanges();

                    db.Checks.Add(new Check
                        {
                            UserID = db.Users.SingleOrDefault(u => u.Username == "user").ID,
                            TotalPrice = 210.0,
                            Date = DateTime.Now.AddMonths(-1).AddDays(-12),
                            Items = new Collection<Item>
                                {
                                    new Item
                                        {
                                            Name = "Phone card",
                                            Amount = 2,
                                            UnitPrice = 50,
                                            Price = 100,
                                            Categories = new Collection<Category>
                                                {
                                                    db.Categories.SingleOrDefault(c => c.CategoryName == "cell phone")
                                                },

                                        },
                                    new Item
                                        {
                                            Name = "Details",
                                            Amount = 1,
                                            UnitPrice = 110.0,
                                            Price = 110.0,
                                            Categories = new Collection<Category>
                                                {
                                                    db.Categories.SingleOrDefault(c => c.CategoryName == "tech"),
                                                    db.Categories.SingleOrDefault(c => c.CategoryName == "required")
                                                }
                                        }
                                }
                        });
                    db.SaveChanges();
                }
            }
            catch (Exception exc)
            {
                // TODO: add log
            }

            base.Seed(db);
        }
    }

    public class MCPDBInitializer : MigrateDatabaseToLatestVersion<MCPDB, MCPDBConfiguration>
    {

    }
}